package com.learn.security.exceptionHandler;


import com.learn.security.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

/**
 * 异常处理器
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handleUserNotExistException(UserNotExistException ex){
        Map<String,Object> map = new HashMap<>();
        map.put("status",HttpStatus.INTERNAL_SERVER_ERROR.value());
        map.put("error",HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        map.put("exception",ex.getClass());
        map.put("message",ex.getMessage());
        map.put("id",ex.getId());
        map.put("timestamp",(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
        return map;
    }



}
